﻿using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using System.Web.Security;

namespace System.Web
{
    public static class UtilityHelper
    {
        #region 去除HTML

        /// <summary>
        /// 去除HTML
        /// </summary>
        /// <returns></returns>
        public static string DelHtml(this string htmlstring)
        {
            htmlstring = Regex.Replace(htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(nbsp|#160);", " ", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            htmlstring = Regex.Replace(htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            htmlstring.Replace("<", "");
            htmlstring.Replace(">", "");
            htmlstring.Replace("\r\n", "");
            return htmlstring;
        }

        #endregion

        #region 截取字符串

        public static string Sub(this string obj, int length, bool bjDelHtml = true)
        {
            var text = obj;
            if (bjDelHtml)
            {
                text = text.DelHtml();
            }
            if (String.IsNullOrEmpty(text.Trim()))
                return String.Empty;
            if (text.Trim().Length < length)
                return text;
            return text.Trim().Substring(0, length) + "...";
        }

        #endregion

        #region Cookies操作

        public static void EmptyCookies(string cookiesName)
        {
            var hc = new HttpCookie(cookiesName) {Expires = DateTime.Now.AddDays(-1)};
            HttpContext.Current.Response.Cookies.Add(hc);
        }

        #endregion

        #region 清除文件夹

        public static void ClearFile(this string filePath)
        {
            var files = Directory.GetFiles(filePath);
            if (files.Length <= 10) return;
            for (var i = 0; i < 10; i++)
            {
                try
                {
                    File.Delete(files[i]);
                }
                catch
                {
                }
            }
        }

        #endregion

        #region Mail

        public static bool MailSend
        (
            string host,
            string userName,
            string password,
            string showName,
            string title,
            string bodyInfo,
            params string[] toAddress
        )
        {
            var msg = new MailMessage();
            foreach (var v in toAddress)
            {
                msg.To.Add(v);
            }
            msg.From = new MailAddress(userName, showName, Encoding.UTF8);
            msg.Subject = title;
            msg.SubjectEncoding = Encoding.UTF8;
            msg.Body = bodyInfo;
            msg.BodyEncoding = Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = MailPriority.High;
            var client = new SmtpClient
                             {
                                 Credentials = new NetworkCredential(userName, password),
                                 Host = host
                             };
            try
            {
                client.Send(msg);
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region 转换

        public static int ToInt(this object obj)
        {
            return Convert.ToInt32(obj);
        }

        public static DateTime ToDateTime(this object obj)
        {
            return Convert.ToDateTime(obj);
        }

        public static Decimal ToDecimal(this object obj)
        {
            return Convert.ToDecimal(obj);
        }

        public static double ToDouble(this object obj)
        {
            return Convert.ToDouble(obj);
        }

        public static bool ToBool(this object obj)
        {
            return Convert.ToBoolean(obj);
        }

        public static string ToJson(this object obj)
        {
            return new JavaScriptSerializer().Serialize(obj);
        }

        #endregion

        #region 加密

        /// <summary> 
        /// 加密数据 
        /// </summary> 
        /// <param name="text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Encrypt(this string text, string sKey = "zhenghaixiao")
        {
            var des = new DESCryptoServiceProvider();
            var inputByteArray = Encoding.Default.GetBytes(text);
            des.Key =
                Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(
                    0, 8));
            des.IV =
                Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(
                    0, 8));
            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            var ret = new StringBuilder();
            foreach (var b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            return ret.ToString();
        }

        #endregion

        #region 解密

        /// <summary> 
        /// 解密数据 
        /// </summary> 
        /// <param name="text"></param> 
        /// <param name="sKey"></param> 
        /// <returns></returns> 
        public static string Decrypt(this string text, string sKey = "zhenghaixiao")
        {
            if (text.Trim() == "")
                return "";
            var des = new DESCryptoServiceProvider();
            var len = text.Length/2;
            var inputByteArray = new byte[len];
            int x;
            for (x = 0; x < len; x++)
            {
                var i = Convert.ToInt32(text.Substring(x*2, 2), 16);
                inputByteArray[x] = (byte) i;
            }
            des.Key =
                Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(
                    0, 8));
            des.IV =
                Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(
                    0, 8));
            var ms = new MemoryStream();
            var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }

        #endregion

        #region 验证

        private static readonly Regex RegPhone = new Regex("^[0-9]+[-]?[0-9]+[-]?[0-9]$");
        private static readonly Regex RegNumber = new Regex("^[0-9]+$");
        private static readonly Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static readonly Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
        private static readonly Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$

        private static readonly Regex RegEmail = new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");
                                      //w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 

        private static readonly Regex RegChzn = new Regex("[\u4e00-\u9fa5]");

        #region 数字字符串检查

        public static bool IsPhone(string inputData)
        {
            var m = RegPhone.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 检查Request查询字符串的键值，是否是数字，最大长度限制
        /// </summary>
        /// <param name="req">Request</param>
        /// <param name="inputKey">Request的键值</param>
        /// <param name="maxLen">最大长度</param>
        /// <returns>返回Request查询字符串</returns>
        public static string FetchInputDigit(HttpRequest req, string inputKey, int maxLen)
        {
            var retVal = string.Empty;
            if (!string.IsNullOrEmpty(inputKey))
            {
                retVal = req.QueryString[inputKey] ?? req.Form[inputKey];
                if (null != retVal)
                {
                    retVal = SqlText(retVal, maxLen);
                    if (!IsNumber(retVal))
                        retVal = string.Empty;
                }
            }
            return retVal ?? (string.Empty);
        }

        /// <summary>
        /// 是否数字字符串
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(string inputData)
        {
            var m = RegNumber.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否数字字符串 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(string inputData)
        {
            var m = RegNumberSign.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮点数
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(string inputData)
        {
            var m = RegDecimal.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(string inputData)
        {
            var m = RegDecimalSign.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 中文检测

        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsHasChzn(string inputData)
        {
            var m = RegChzn.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 邮件地址

        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(string inputData)
        {
            var m = RegEmail.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 日期格式判断

        /// <summary>
        /// 日期格式字符串判断
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(string str)
        {
            try
            {
                if (!string.IsNullOrEmpty(str))
                {
                    DateTime.Parse(str);
                    return true;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region 其他

        /// <summary>
        /// 检查字符串最大长度，返回指定长度的串
        /// </summary>
        /// <param name="sqlInput">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns></returns>			
        public static string SqlText(string sqlInput, int maxLength)
        {
            if (!string.IsNullOrEmpty(sqlInput))
            {
                sqlInput = sqlInput.Trim();
                if (sqlInput.Length > maxLength) //按最大长度截取字符串
                    sqlInput = sqlInput.Substring(0, maxLength);
            }
            return sqlInput;
        }

        /// <summary>
        /// 字符串编码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static string HtmlEncode(string inputData)
        {
            return HttpUtility.HtmlEncode(inputData);
        }

        //字符串清理
        public static string InputText(string inputString, int maxLength)
        {
            var retVal = new StringBuilder();

            // 检查是否为空
            if (!string.IsNullOrEmpty(inputString))
            {
                inputString = inputString.Trim();

                //检查长度
                if (inputString.Length > maxLength)
                    inputString = inputString.Substring(0, maxLength);

                //替换危险字符
                foreach (var t in inputString)
                {
                    switch (t)
                    {
                        case '"':
                            retVal.Append("&quot;");
                            break;
                        case '<':
                            retVal.Append("&lt;");
                            break;
                        case '>':
                            retVal.Append("&gt;");
                            break;
                        default:
                            retVal.Append(t);
                            break;
                    }
                }
                retVal.Replace("'", " "); // 替换单引号
            }
            return retVal.ToString();
        }

        /// <summary>
        /// 转换成 HTML code
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Encode(string str)
        {
            str = str.Replace("&", "&amp;");
            str = str.Replace("'", "''");
            str = str.Replace("\"", "&quot;");
            str = str.Replace(" ", "&nbsp;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\n", "<br>");
            return str;
        }

        /// <summary>
        ///解析html成 普通文本
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Decode(string str)
        {
            str = str.Replace("<br>", "\n");
            str = str.Replace("&gt;", ">");
            str = str.Replace("&lt;", "<");
            str = str.Replace("&nbsp;", " ");
            str = str.Replace("&quot;", "\"");
            return str;
        }

        public static string SqlTextClear(string sqlText)
        {
            if (sqlText == null)
            {
                return null;
            }
            if (sqlText == "")
            {
                return "";
            }
            sqlText = sqlText.Replace(",", ""); //去除,
            sqlText = sqlText.Replace("<", ""); //去除<
            sqlText = sqlText.Replace(">", ""); //去除>
            sqlText = sqlText.Replace("--", ""); //去除--
            sqlText = sqlText.Replace("'", ""); //去除'
            sqlText = sqlText.Replace("\"", ""); //去除"
            sqlText = sqlText.Replace("=", ""); //去除=
            sqlText = sqlText.Replace("%", ""); //去除%
            sqlText = sqlText.Replace(" ", ""); //去除空格
            return sqlText;
        }

        #endregion

        #region 是否由特定字符组成

        public static bool IsContainSameChar(string strInput)
        {
            var charInput = string.Empty;
            if (!string.IsNullOrEmpty(strInput))
            {
                charInput = strInput.Substring(0, 1);
            }
            return IsContainSameChar(strInput, charInput);
        }

        public static bool IsContainSameChar(string strInput, string charInput)
        {
            if (string.IsNullOrEmpty(charInput))
            {
                return false;
            }
            var regNumber = new Regex(string.Format("^([{0}])+$", charInput));
            //Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput));
            var m = regNumber.Match(strInput);
            return m.Success;
        }

        #endregion

        #region 检查输入的参数是不是某些定义好的特殊字符：这个方法目前用于密码输入的安全检查

        /// <summary>
        /// 检查输入的参数是不是某些定义好的特殊字符：这个方法目前用于密码输入的安全检查
        /// </summary>
        public static bool IsContainSpecChar(string strInput)
        {
            var list = new[] {"123456", "654321"};
            return list.Any(t => strInput == t);
        }

        #endregion

        #endregion

        #region HzToPy

        private static readonly int[] Pyvalue = new[]
                                                    {
                                                        -20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257,
                                                        -20242, -20230, -20051, -20036, -20032, -20026,
                                                        -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775,
                                                        -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728,
                                                        -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484,
                                                        -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263,
                                                        -19261, -19249, -19243, -19242, -19238, -19235, -19227, -19224,
                                                        -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996,
                                                        -18977, -18961, -18952, -18783, -18774, -18773, -18763, -18756,
                                                        -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526,
                                                        -18518, -18501, -18490, -18478, -18463, -18448, -18447, -18446,
                                                        -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183,
                                                        -18181, -18012, -17997, -17988, -17970, -17964, -17961, -17950,
                                                        -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730,
                                                        -17721, -17703, -17701, -17697, -17692, -17683, -17676, -17496,
                                                        -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202,
                                                        -17185, -16983, -16970, -16942, -16915, -16733, -16708, -16706,
                                                        -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459,
                                                        -16452, -16448, -16433, -16429, -16427, -16423, -16419, -16412,
                                                        -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205,
                                                        -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959,
                                                        -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878,
                                                        -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640,
                                                        -15631, -15625, -15454, -15448, -15436, -15435, -15419, -15416,
                                                        -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362,
                                                        -15183, -15180, -15165, -15158, -15153, -15150, -15149, -15144,
                                                        -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117,
                                                        -15110, -15109, -14941, -14937, -14933, -14930, -14929, -14928,
                                                        -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889,
                                                        -14882, -14873, -14871, -14857, -14678, -14674, -14670, -14668,
                                                        -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399,
                                                        -14384, -14379, -14368, -14355, -14353, -14345, -14170, -14159,
                                                        -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123,
                                                        -14122, -14112, -14109, -14099, -14097, -14094, -14092, -14090,
                                                        -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905,
                                                        -13896, -13894, -13878, -13870, -13859, -13847, -13831, -13658,
                                                        -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391,
                                                        -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329,
                                                        -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095,
                                                        -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871,
                                                        -12860, -12858, -12852, -12849, -12838, -12831, -12829, -12812,
                                                        -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346,
                                                        -12320, -12300, -12120, -12099, -12089, -12074, -12067, -12058,
                                                        -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604,
                                                        -11589, -11536, -11358, -11340, -11339, -11324, -11303, -11097,
                                                        -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024,
                                                        -11020, -11019, -11018, -11014, -10838, -10832, -10815, -10800,
                                                        -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331,
                                                        -10329, -10328, -10322, -10315, -10309, -10307, -10296, -10281,
                                                        -10274, -10270, -10262, -10260, -10256, -10254
                                                    };

        private static readonly string[] Pystr = new[]
                                                     {
                                                         "a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao"
                                                         , "bei", "ben", "beng", "bi", "bian", "biao",
                                                         "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang",
                                                         "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao",
                                                         "che", "chen",
                                                         "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan",
                                                         "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu",
                                                         "cuan", "cui",
                                                         "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng",
                                                         "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du"
                                                         , "duan",
                                                         "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang",
                                                         "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan",
                                                         "gang", "gao",
                                                         "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai",
                                                         "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han",
                                                         "hang",
                                                         "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua",
                                                         "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia",
                                                         "jian",
                                                         "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju",
                                                         "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke",
                                                         "ken",
                                                         "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang",
                                                         "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le",
                                                         "lei",
                                                         "leng", "li", "lia", "lian", "liang", "liao", "lie", "lin",
                                                         "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun",
                                                         "luo",
                                                         "ma", "mai", "man", "mang", "mao", "me", "mei", "men", "meng",
                                                         "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou",
                                                         "mu",
                                                         "na", "nai", "nan", "nang", "nao", "ne", "nei", "nen", "neng",
                                                         "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu",
                                                         "nong",
                                                         "nu", "nv", "nuan", "nue", "nuo", "o", "ou", "pa", "pai", "pan"
                                                         , "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao",
                                                         "pie",
                                                         "pin", "ping", "po", "pu", "qi", "qia", "qian", "qiang", "qiao"
                                                         , "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que",
                                                         "qun",
                                                         "ran", "rang", "rao", "re", "ren", "reng", "ri", "rong", "rou",
                                                         "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang",
                                                         "sao", "se", "sen", "seng", "sha", "shai", "shan", "shang",
                                                         "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua",
                                                         "shuai", "shuan", "shuang", "shui", "shun", "shuo", "si",
                                                         "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai",
                                                         "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", "tie"
                                                         , "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo",
                                                         "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu",
                                                         "xi", "xia", "xian", "xiang", "xiao", "xie", "xin", "xing",
                                                         "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang"
                                                         , "yao", "ye", "yi", "yin", "ying", "yo", "yong", "you",
                                                         "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao",
                                                         "ze", "zei", "zen", "zeng", "zha", "zhai", "zhan", "zhang",
                                                         "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu",
                                                         "zhua", "zhuai", "zhuan", "zhuang", "zhui", "zhun", "zhuo",
                                                         "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"
                                                     };

        /// <summary>
        /// 获取拼音
        /// </summary>
        /// <param name="str">汉字</param>
        /// <returns></returns>
        public static string HzToPy(this string str)
        {
            var returnstr = "";
            var nowchar = str.ToCharArray();
            for (var j = 0; j < nowchar.Length; j++)
            {
                var btchk = Encoding.Default.GetBytes(nowchar[j].ToString());
                if (btchk.Length == 1)
                {
                    returnstr += nowchar[j].ToString();
                    continue;
                }
                var array = btchk;
                //array = System.Text.Encoding.Default.GetBytes(nowchar[j].ToString());
                int i1 = (array[0]);
                int i2 = (array[1]);
                var chrasc = i1*256 + i2 - 65536;
                if (chrasc > 0 && chrasc < 160)
                {
                    returnstr += nowchar[j];
                }
                else
                {
                    for (var i = (Pyvalue.Length - 1); i >= 0; i--)
                    {
                        if (Pyvalue[i] > chrasc) continue;
                        returnstr += Pystr[i];
                        break;
                    }
                }
            }
            return returnstr;
        }

        /// <summary>
        /// 获取拼音首字母
        /// </summary>
        /// <param name="str">汉字</param>
        /// <returns></returns>
        public static string HzToPySzm(this IEnumerable<char> str)
        {
            var tempStr = "";
            foreach (var c in str)
            {
                if (c >= 33 && c <= 126)
                {
                    tempStr += c.ToString(); //字母和符号原样保留 
                }
                else
                {
                    if (c != 32) //如果不是空格，那么转换
                    {
                        tempStr += GetPyChar(c.ToString()); //累加拼音声母 
                    }
                    else //如果是空格那么在字符串中间+上一个空字符
                    {
                        tempStr += " ";
                    }
                }
            }
            return tempStr;
        }

        private static string GetPyChar(string c)
        {
            var array = Encoding.Default.GetBytes(c);
            var i = (short) (array[0] - '\0')*256 + ((short) (array[1] - '\0'));

            if (i < 0xB0A1) return "";
            if (i < 0xB0C5) return "a";
            if (i < 0xB2C1) return "b";
            if (i < 0xB4EE) return "c";
            if (i < 0xB6EA) return "d";
            if (i < 0xB7A2) return "e";
            if (i < 0xB8C1) return "f";
            if (i < 0xB9FE) return "g";
            if (i < 0xBBF7) return "h";
            if (i < 0xBFA6) return "j";
            if (i < 0xC0AC) return "k";
            if (i < 0xC2E8) return "l";
            if (i < 0xC4C3) return "m";
            if (i < 0xC5B6) return "n";
            if (i < 0xC5BE) return "o";
            if (i < 0xC6DA) return "p";
            if (i < 0xC8BB) return "q";
            if (i < 0xC8F6) return "r";
            if (i < 0xCBFA) return "s";
            if (i < 0xCDDA) return "t";
            if (i < 0xCEF4) return "w";
            if (i < 0xD1B9) return "x";
            if (i < 0xD4D1) return "y";
            return i < 0xD7FA ? "z" : "";
        }

        #endregion

        #region file

        public static string MoveFile(this string filePath, string toPath)
        {
            try
            {
                filePath = ("~" + filePath).Replace("~~", "~");
                toPath = ("~" + toPath).Replace("~~", "~");
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(toPath)))
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(toPath));
                }
                File.Move(HttpContext.Current.Server.MapPath(filePath),
                          HttpContext.Current.Server.MapPath(toPath + "/" + Path.GetFileName(filePath)));
                return (toPath.Replace("~", "") + "/" + Path.GetFileName(filePath)).Replace("//", "/");
            }
            catch
            {
                return "";
            }
        }

        public static string CopyFile(this string filePath, string toPath)
        {
            try
            {
                filePath = ("~" + filePath).Replace("~~", "~");
                toPath = ("~" + toPath).Replace("~~", "~");
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(toPath)))
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(toPath));
                }
                File.Copy(HttpContext.Current.Server.MapPath(filePath),
                          HttpContext.Current.Server.MapPath(toPath + "/" + Path.GetFileName(filePath)));
                return (toPath.Replace("~", "") + "/" + Path.GetFileName(filePath)).Replace("//", "/");
            }
            catch
            {
                return "";
            }
        }

        public static void DeleteFile(this string filePath)
        {
            try
            {
                filePath = ("~" + filePath).Replace("~~", "~");
                File.Delete(HttpContext.Current.Server.MapPath(filePath));
            }
            catch
            {
            }
        }

        public static string FileName(this string filePath)
        {
            return Path.GetFileName(filePath);
        }

        #endregion

        #region url

        public static string AddUrlQuery(this string query)
        {
            var ss = HttpContext.Current.Request.Url.PathAndQuery;
            if (ss.IndexOf("?") < 0)
            {
                ss += "?" + query;
            }
            else
            {
                ss += "&" + query;
            }
            return ss;
        }

        #endregion

        #region 生成缩略图

        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImagePath">源图路径（物理路径）</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="toPath"></param>
        public static string MakeThumbnail(this string originalImagePath, int width, int height, string toPath)
        {
            originalImagePath = HttpContext.Current.Server.MapPath("~" + originalImagePath);
            if (!Directory.Exists(HttpContext.Current.Server.MapPath(toPath)))
            {
                Directory.CreateDirectory(HttpContext.Current.Server.MapPath(toPath));
            }
            var bmp = new Bitmap(originalImagePath);
            var towidth = width;
            var toheight = height;
            int x;
            int y;
            int ow;
            int oh;
            if (bmp.Width/(double) bmp.Height > towidth/(double) toheight)
            {
                oh = bmp.Height;
                ow = bmp.Height*towidth/toheight;
                y = 0;
                x = (bmp.Width - ow)/2;
            }
            else
            {
                ow = bmp.Width;
                oh = bmp.Width*height/towidth;
                x = 0;
                y = (bmp.Height - oh)/2;
            }

            var bitmap = new Bitmap(towidth, toheight);
            var g = Graphics.FromImage(bitmap);
            g.InterpolationMode = InterpolationMode.High;
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(bmp, new Rectangle(0, 0, towidth, toheight), new Rectangle(x, y, ow, oh), GraphicsUnit.Pixel);
            bmp.Dispose();
            var thumbnailPath = toPath + DateTime.Now.ToString("yyyyMMddHHmmssfff") +
                                   Path.GetExtension(originalImagePath);
            try
            {
                bitmap.Save(HttpContext.Current.Server.MapPath(thumbnailPath));
                bitmap.Dispose();
                g.Dispose();
                return thumbnailPath.Substring(1);
            }
            catch
            {
                bitmap.Dispose();
                g.Dispose();
                return "";
            }
        }

        #endregion
    }
}